package com.example.hutool;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RegexTest {

    public static final List<String> list = Arrays.asList(
        "100.58.189.144 - [2024-03-25 17:21:45] /login 204",
        "100.58.189.145 - [2024-03-26 17:21:46] /logout 204",
        "100.58.189.146 - [2024-03-27 17:21:47] /user 204",
        "100.58.189.147 - [2024-03-28 17:21:48] /logout 204",
        "100.58.189.148 - [2024-03-29 17:21:49] /login 204",
        "100.58.189.149 - [2024-03-30 17:21:50] /login 204"
    );

    public static void main(String[] args) {
        // 正则匹配组 用括号分组
        Pattern compile = Pattern.compile("(\\S+) - \\[(.+)] (.+) (.+)");
        // 统计地址访问次数
        Map<String, Long> map = list.stream()
            .map(compile::matcher)
            // 过滤掉不匹配的
            .filter(Matcher::find)
            // 匹配组索引从0开始
            .map(m -> m.group(3))
            .collect(Collectors.groupingBy(s -> s, Collectors.counting()));

        map.forEach((k ,v) -> System.out.println(k + ": " + v));

    }
}